package com.bw.mallshop.mvvm.model;

import com.bw.common.BaseApplication;
import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.model.BaseModel;
import com.bw.common.network.HttpType;
import com.bw.common.network.RetrofitFactory;
import com.bw.mallshop.App;
import com.bw.mallshop.entity.RegisterEntity;
import com.bw.mallshop.entity.UserEntity;
import com.bw.mallshop.entity.UserInsertEntity;
import com.bw.mallshop.repository.ApiService;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.annotations.NonNull;

public class RegisterModel extends BaseModel {
    //注册接口
   public Observable<BaseEntity>requestRegister(Map<String,Object>parames){
        Observable<RegisterEntity>requestRegister = RetrofitFactory.factory(HttpType.TOKENSIGN)
                .createRetrofit().create(ApiService.class).requestRegister(createBody(parames));
        return changeObservable(requestRegister);
    }
    //用户名密码登录
    public Observable<BaseEntity>requestLogin(Map<String,Object>parames){
        Observable<UserEntity>requestlogin = RetrofitFactory.factory(HttpType.TOKENSIGN)
                .createRetrofit().create(ApiService.class).requestLogin(createBody(parames));
        return changeObservable(requestlogin);
    }
    //添加登录成功得数据
    public Observable<BaseEntity>insertUser(UserEntity userEntity){
        Observable<UserInsertEntity>observable = Observable.create(new ObservableOnSubscribe<UserInsertEntity>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<UserInsertEntity> emitter) throws Exception {
                App app = (App) BaseApplication.getInstance();
                app.db.userDao().insertUsers(userEntity.data);
                UserInsertEntity userInsertEntity = new UserInsertEntity();
                userInsertEntity.status = 0;
                userInsertEntity.message = "插入成功";
                emitter.onNext(userInsertEntity);
                emitter.onComplete();
            }
        });
        return changeObservable(observable);
    }

}
